﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using AppToolkit.WPF.UI.Converters;
using System.Windows;
using System.Windows.Threading;
using System.Collections.ObjectModel;
using System.IO.Packaging;

namespace AppToolkit.WPF.Tests.UI.Converters
{
    [TestFixture]
    public class EnumToBitmapResourceConverterTests
    {
        #region Setup / Teardown
        [TearDown]
        public void Dispose()
        {
            if (Dispatcher.CurrentDispatcher != null && !Dispatcher.CurrentDispatcher.HasShutdownStarted)
            {
                Dispatcher.CurrentDispatcher.InvokeShutdown();
            }
        }
        #endregion
        
        [Test]
        public void Can_set_base_uri()
        {
            EnumToBitmapResourceConverter target = new EnumToBitmapResourceConverter();
            var map = new Collection<MappingElement>();
            const string STR_Uri = "/ResourceFile.xaml";
            var uri = new Uri(STR_Uri, UriKind.Relative);

            map.Add(new MappingElement() { TypeName = "Value1", ResourceUri = uri });
            target.Mappings = map;

            var result = target.Convert(TestEnum.Value1, typeof(Uri), null, null);

            Uri typedResult = result as Uri;

            Assert.IsNotNull(typedResult);
            Assert.AreEqual(uri, typedResult);
        }

        [Test]
        public void Invalid_configuration_results_in_Unset()
        {
            EnumToBitmapResourceConverter target = new EnumToBitmapResourceConverter();
            var map = new Collection<MappingElement>();
            const string STR_Uri = "/ResourceFile.xaml";
            var uri = new Uri(STR_Uri, UriKind.Relative);

            map.Add(new MappingElement() { TypeName = "Value1", ResourceUri = uri });
            target.Mappings = map;

            var result = target.Convert(TestEnum.Value2, typeof(Uri), null, null);
            
            Assert.AreEqual(DependencyProperty.UnsetValue, result);
        }



        [Test, Explicit, RequiresSTA]
        public void Interactive_EnumToImageResourceConverterTest()
        {
            Window wnd = new Window();
            var ui = new EnumToImageResourceConverterUI();
            ui.HorizontalContentAlignment = HorizontalAlignment.Stretch;
            ui.VerticalContentAlignment = VerticalAlignment.Stretch;
            wnd.Content = ui;

            wnd.ShowDialog();
        }
    }
}
